#include "Rectangle.h"

Rectangle::Rectangle(){}


Rectangle::Rectangle(std::string id, Point vertex, int width, int height, Color *borderColor):width(width),height(height){
	this->borderColor = borderColor;
	this->fillColor=NULL;
	this->texture=NULL;
	this->id=id;
	this->setLimits(vertex);
}

Rectangle::Rectangle(std::string id, Point vertex,int width, int height, Color *lineColor, Color *fillColor):width(width),height(height){
	if(lineColor==NULL)
		this->borderColor=new Color(*fillColor);
	else 
		this->borderColor=lineColor;
	this->id=id;
	this->fillColor=fillColor;
	this->texture=NULL;
	this->setLimits(vertex);
}

Rectangle::Rectangle(std::string id, Point vertex,int width, int height, Color *borderColor, Texture *texture):width(width),height(height){
	this->borderColor = borderColor;
	this->id=id;
	this->fillColor=NULL;
	this->setLimits(vertex);
	this->texture=texture;
}

Rectangle::Rectangle(std::string id, Point vertex,int width, int height, Texture* texture):width(width),height(height){
	this->borderColor = NULL;
	this->id=id;
	this->fillColor=NULL;
	this->setLimits(vertex);
	this->texture=texture;
}

void Rectangle::setLimits(Point vertex){
	//seteo los cuatro puntros del rectangulo
	Point aux(0,0);
	aux.setY(this->height);
	pointA = vertex - aux;
	aux.setY(0);
	aux.setX(this->width);
	pointB = pointA + aux;   
	pointC = vertex;
	pointD = vertex + aux;
}

void Rectangle::draw() throw (DrawingExcep){
	if(fillColor!=NULL)
		SurfaceContainer::getInstance()->fillRectangle(this->pointC,this->width,this->height,this->fillColor->getSDLColor());
	if(texture!=NULL)
		drawTextured();
	if(borderColor!=NULL)
		SurfaceContainer::getInstance()->drawRectangle(pointA,pointB,pointD,pointC,this->borderColor->getSDLColor());
}

void Rectangle::drawTextured(){
	int posicion =0;
	//seteo los puntos de la imagen, textura
	Point aux(0,0);
	Point a_bmp = aux;
	aux.setX(texture->getImage()->getImageWidth());
	Point b_bmp = aux;
	aux.setX(0);
	aux.setY(texture->getImage()->getImageHeight());
	Point c_bmp = aux;
	aux.setX(texture->getImage()->getImageWidth());
	Point d_bmp = aux;
	
	if (SurfaceContainer::getInstance()->pointInScreen(pointC)){
	//if (SurfaceContainer::getInstance()->acceptLimits(this->pointC,this->width,this->height)){
		SurfaceContainer::getInstance()->fillTexture(this->texture->getImage(),pointA,pointD,pointC,a_bmp,d_bmp,c_bmp,posicion);
		SurfaceContainer::getInstance()->fillTexture(this->texture->getImage(),pointA,pointB,pointD,a_bmp,b_bmp,d_bmp,posicion);
	}
	else 
		throw DrawingExcep(MsjExcep::NotAcceptLimits(pointC));
}

std::string Rectangle::getCreationLog(){
	std::string msj = "Se creo un Rectangulo id: '";
	std::stringstream ss; 
	ss << id;
	msj+= ss.str();
	msj.append("'. Base: ");
	ss.str("");
	ss<< width;
	msj += ss.str();
	msj.append(". Altura: ");
	ss.str("");
	ss<< height;
	msj += ss.str();
	msj.append(" , Vertice: [");
	ss.str("");
	ss<< pointC.getX();
	msj += ss.str();
	ss.str("");
	msj.append(",");
	ss<< pointC.getY();
	msj += ss.str();
	msj.append("]");
	return msj;
}

Rectangle::~Rectangle(){}